package cn.lingyangwl.agile.system.manager;

import cn.lingyangwl.agile.system.constants.SysErrorEnum;
import cn.lingyangwl.agile.system.model.entity.SysTenant;
import cn.lingyangwl.agile.system.mapper.SysTenantMapper;
import cn.lingyangwl.agile.system.model.rqrs.tenant.TenantBase;
import cn.lingyangwl.agile.system.model.rqrs.tenant.TenantUpdateReq;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class SysTenantManager extends ServiceImpl<SysTenantMapper, SysTenant> {

    public void validSaveTenant(TenantBase req) {
        LambdaQueryWrapper<SysTenant> lqw = SysTenant.lqw()
                .eq(SysTenant::getSceneId, req.getSceneId())
                .or(StringUtils.isNotEmpty(req.getDomain()))
                .eq(StringUtils.isNotEmpty(req.getDomain()), SysTenant::getDomain, req.getDomain());
        List<SysTenant> list = this.list(lqw);
        doWriteValid(req, list);
    }

    public void doWriteValid(TenantBase req, List<SysTenant> list) {
        if (StringUtils.isNotEmpty(req.getDomain())) {
            if (list.stream().anyMatch(e -> req.getDomain().equals(e.getDomain()))) {
                throw new BizException(SysErrorEnum.TENANT_DOMAIN_EXIST);
            }
        }

        if (Boolean.TRUE.equals(req.getIsMgtSceneTenant())) {
            SysTenant ret = list.stream().filter(e -> req.getSceneId().equals(e.getSceneId()) && e.getIsMgtSceneTenant())
                    .findFirst().orElse(null);
            if (Objects.nonNull(ret)) {
                throw new BizException("[{}] 已是管理租户, 无需再次添加", ret.getName());
            }
        }
    }

    public void validUpdateTenant(TenantUpdateReq req) {
        LambdaQueryWrapper<SysTenant> lqw = SysTenant.lqw().ne(SysTenant::getId, req.getId())
                .eq(SysTenant::getSceneId, req.getSceneId())
                .or(StringUtils.isNotEmpty(req.getDomain()))
                .eq(StringUtils.isNotEmpty(req.getDomain()), SysTenant::getDomain, req.getDomain());

        doWriteValid(req, this.list(lqw));
    }

    public long countByPackageId(Long packageId) {
        LambdaQueryWrapper<SysTenant> lqw = SysTenant.lqw().eq(SysTenant::getPackageId, packageId);
        return this.count(lqw);
    }

    public List<SysTenant> listByPackageId(Long packageId) {
        LambdaQueryWrapper<SysTenant> lqw = SysTenant.lqw().eq(SysTenant::getPackageId, packageId);
        return this.list(lqw);
    }

    public List<SysTenant> listSubTenant(String mainTenantId) {
        LambdaQueryWrapper<SysTenant> lqw = SysTenant.lqw().eq(SysTenant::getMainTenantId, mainTenantId)
                .ne(SysTenant::getId, mainTenantId);
        return this.list(lqw);
    }

    public void updateSubTenantPackageId(String mainTenantId, Long subPackageId) {
        LambdaUpdateWrapper<SysTenant> lqw = SysTenant.luw().eq(SysTenant::getMainTenantId, mainTenantId)
                .ne(SysTenant::getId, mainTenantId).set(SysTenant::getPackageId, subPackageId);
        this.update(lqw);
    }

}
